<?php
namespace app\admin\model;

use think\Model;
use think\Db;

/**
 * 回调重推
 * @package app\admin\model
 */
class ResendTask extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'resend_task';

    public function getList($request)
    {
        $war_config = $this
            ->alias('res')
            ->field('res.*,cha.company_name')
            ->join('bus_channel cha','cha.channel_id = res.cid','LEFT')
            ->paginate($request['limit'], false,['query' => $request])
            ->each(function($item, $key){

                $item->allnum     = 0;
                $item->successnum = 0;
                $item->falinum    = 0;
                $item->dainum     = 0;

                switch ($item->task_type) {
                    case 1:

                        $resend_callback = Db::name('resend_callback')->field('count(id) allnum,deal')->where('task_id',$item->id)->group('deal')->select();

                        foreach ($resend_callback as $k => $v) {

                            switch ($v['deal']) {
                                case 0:
                                    //待处理
                                    $item->dainum += $v['allnum'];
                                    break;
                                case 5:
                                    //成功
                                    $item->successnum += $v['allnum'];
                                    break;
                                case 6:
                                    //失败
                                    $item->falinum += $v['allnum'];
                                    break;

                                default:
                                    # code...
                                    break;
                            }
                            //总数
                            $item->allnum += $v['allnum'];
                        }

                        $item->task_type = '回调';
                        break;
                    case 2:
                        $resend_callback = Db::name('resend_deliver')->field('count(id) allnum,del')->where('task_id',$item->id)->group('del')->select();

                        foreach ($resend_callback as $k => $v) {

                            switch ($v['del']) {
                                case 0:
                                    //待处理
                                    $item->dainum += $v['allnum'];
                                    break;
                                case 5:
                                    //成功
                                    $item->successnum += $v['allnum'];
                                    break;
                                case 6:
                                    //失败
                                    $item->falinum += $v['allnum'];
                                    break;

                                default:
                                    # code...
                                    break;
                            }
                            //总数
                            $item->allnum += $v['allnum'];
                        }

                        $item->task_type = 'deliver';
                        break;

                    default:
                        # code...
                        break;
                }
                switch ($item->record_status) {
                    case 0:
                        $item->record_status = '全部';
                        break;
                    case 1:
                        $item->record_status = '未回调';
                        break;
                    case 2:
                        $item->record_status = '已回调';
                        break;

                    default:
                        break;
                }


            });

            return $war_config->toarray();
    }

    /**
     * 添加
     * @Author   lf
     * @DateTime 2019-06-06T16:44:45+0800
     */
    public function addCallBack($request)
    {
        $bus_order = Db::table('bus_order')->alias('o')
        ->field('c.phone,c.status,c.msg,a.table_order_id,o.order_id,o.cid,o.channel_order_id,o.port_type,o.resulturl')
        ->join('bus_add_order a' , 'o.id=a.table_order_id')
        ->join('bus_callback c' , 'a.bus_plat_id=c.trade_id')
        ->where('o.cid' , $request['cid'])
        ->where('o.create_time','between time', [$request['time_begin'], $request['time_end']])
        ->select();

        if($bus_order)
        {
            $insertGetId = Db::table('resend_task')->insertGetId([
                'guid'          => uniqid(),
                'task_type'     => $request['task_type'],
                'cid'           => $request['cid'],
                'record_status' => $request['record_status'],
                'time_begin'    => $request['time_begin'],
                'time_end'      => $request['time_end'],
                'create_time'   => date('Y-m-d H:i:s'),
                ]);

            $addData = [];
            //添加的数据
            foreach ($bus_order as $key => $value) {

                $addData[] = [
                    'task_id'          => $insertGetId,
                    'phone'            => $value['phone'],
                    'order_id'         => $value['order_id'],
                    'channel_order_id' => $value['channel_order_id'],
                    'cid'              => $value['cid'],
                    'status'           => $value['status'],
                    'msg'              => $value['msg'],
                    'port_type'        => $value['port_type'],
                    'resulturl'        => $value['resulturl'],
                    'deal'             => 0,
                ];
            }

            $addnum = Db::name('resend_callback')->insertAll($addData);

             trace('任务记录：'.$addnum,'info');

            return 'success';
        }else{
            return '没有找到数据';
        }
    }
    /**
     * 添加
     * @Author   lf
     * @DateTime 2019-06-06T16:44:45+0800
     */
    public function addDeliver($request)
    {
        $where = [];
        switch ($request['record_status']) {
            case '1':
                $where['status'] = 0;
                break;
            case '2':
                $where['status'] = ['<>',0];
                break;

            default:
                # code...
                break;
        }
        $bus_order = Db::table('cmpp_server_deliver')
        ->field(true)
        ->where('cid' , $request['cid'])
        ->where($where)
        ->where('time','between time', [$request['time_begin'], $request['time_end']])
        ->select();

        if($bus_order)
        {
            $insertGetId = Db::table('resend_task')->insertGetId([
                'guid'          => uniqid(),
                'task_type'     => $request['task_type'],
                'cid'           => $request['cid'],
                'record_status' => $request['record_status'],
                'time_begin'    => $request['time_begin'],
                'time_end'      => $request['time_end'],
                'create_time'   => date('Y-m-d H:i:s'),
                ]);

            $addData = [];
            //添加的数据
            foreach ($bus_order as $key => $value) {

                $addData[] = [
                    'task_id'   => $insertGetId,
                    'phone'     => $value['phone'],
                    'squenceid' => $value['squenceid'],
                    'msgid'     => $value['msgid'],
                    'status'    => $value['status'],
                    'time'      => $value['time'],
                    'msg'       => $value['msg'],
                    'cid'       => $value['cid'],
                    'del'       => 0,
                ];
            }

            $addnum = Db::name('resend_deliver')->insertAll($addData);

             trace('任务记录：'.$addnum,'info');

            return 'success';
        }else{
            return '没有找到数据';
        }
    }
}